package be.bonamis.umpire.ui.client.activities;


import java.util.ArrayList;
import java.util.List;

import be.bonamis.projects.umpire.business.actions.GetFPReportA;
import be.bonamis.projects.umpire.business.actions.GetFPReportR;
import be.bonamis.projects.umpire.business.actions.GetMonthsA;
import be.bonamis.projects.umpire.business.actions.GetMonthsR;
import be.bonamis.umpire.ui.client.ClientFactory;
import be.bonamis.umpire.ui.client.mvp.HistoryView;
import be.bonamis.umpire.ui.client.places.DispoPlace;
import be.bonamis.umpire.ui.client.places.HistoryPlace;
import be.bonamis.umpire.ui.client.places.WhoPlace;
import be.credoc.common.gwt.actions.client.ActionServiceWrapper;
import be.credoc.common.gwt.actions.client.OnResponseCommand;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HistoryActivity extends BaseActivity<HistoryPlace> implements HistoryView.Presenter {

	private HistoryView historyView;
	private ActionServiceWrapper asw;

	public HistoryActivity(HistoryPlace place, ClientFactory clientFactory) {
		super(place, clientFactory);
		this.clientFactory = clientFactory;
		this.asw = clientFactory.getActionServiceWrapper();
		this.place = place;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		historyView = clientFactory.getHistoryView();
		if (place.getUmpire() != null) {
			historyView.setValues(place.getUmpire(),place.getGames());
		}
		historyView.setPresenter(this);
		panel.setWidget(historyView.asWidget());
	}	
		
	@Override
	public void dispoButtonClicked() {
		GetMonthsA<GetMonthsR> action = new GetMonthsA<GetMonthsR>();
		asw.execute(action, new OnResponseCommand<GetMonthsR>() {
			@Override
			public void execute() {
				clientFactory.getPlaceController().goTo(
						new DispoPlace("dispo", historyView.getUmpire(), response.getSetDates()));
			}
		});

	}

	@Override
	public void whoButtonClicked() {
		clientFactory.getPlaceController().goTo(new WhoPlace("who", historyView.getUmpire()));
	}

	@Override
	public void putResponsesInDB(Integer umpireId, long gameId, VerticalPanel reportPanel,
			List<HorizontalPanel> panels, List<TextBox> tBoxes) {
		List<Integer> homePoints = new ArrayList<Integer>();
		List<Integer> awayPoints = new ArrayList<Integer>();
		
		RadioButton button = (RadioButton) panels.get(0).getWidget(0);
		manipulateButton(homePoints, button);
		button = (RadioButton) panels.get(1).getWidget(0);
		manipulateButton(awayPoints, button);
		
		homePoints.add(Integer.parseInt(tBoxes.get(0).getValue()));
		awayPoints.add(Integer.parseInt(tBoxes.get(1).getValue()));

		button = (RadioButton) panels.get(2).getWidget(0);
		manipulateButton(homePoints, button);
		button = (RadioButton) panels.get(3).getWidget(0);
		manipulateButton(awayPoints, button);	
		
		GetFPReportA<GetFPReportR> action = new GetFPReportA<GetFPReportR>();
		action.setAwayPoints(awayPoints);
		action.setHomePoints(homePoints);
		action.setGameId((int) gameId);
		action.setUmpireId(umpireId);
		asw.execute(action, new OnResponseCommand<GetFPReportR>() {
			@Override
			public void execute() {
				historyView.updateScores(response.getList());
			}
		});
		
	}

	private void manipulateButton(List<Integer> homePoints, RadioButton button) {
		if (button.getValue()) {
			homePoints.add(5);
		} else {
			homePoints.add(0);
		}
	}
	
}
